// pages/biaoqian/biaoqian.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectzichan: [],
    selectzhaobiao: [],
    selectgongqiu: [],
    selectNum: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    //定义导航
    that.setData({
      navHeight: app.globalData.navHeight, //导航栏高度
      navTop: app.globalData.navTop, //导航栏距顶部距离
      navObj: app.globalData.navObj, //胶囊的高度
      navObjWid: app.globalData.navObjWid, //胶囊宽度+距右距离

    })
    that.queryBiaoqian()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 查询标签
  queryBiaoqian(e) {
    let that = this
    app.getDaoPost('api/FeijiuApp/Feijiu_Get_TagClassList', {}, r => {
      console.log(r, '查询标签')
      if (r.code == 200) {
        that.setData({
          biaoqianList: r.data
        })
      } else {
        wx.showToast({
          title: r.message,
          icon: 'none'
        })
      }
    })
  },
  // 点击选择标签
  selectbiaiqan(e) {
    let classid = e.currentTarget.dataset.classid,
      index2 = e.currentTarget.dataset.index2,
      type = e.currentTarget.dataset.type,
      biaoqianList = this.data.biaoqianList,
      index = e.currentTarget.dataset.index,
      selectNum = this.data.selectNum
    // if (type.indexOf('ZCCList') != -1) { //找到了资产处置
    //  biaoqianList
    // } else if (type.indexOf('GGCList') != -1) { //招标拍卖

    // } else if (type.indexOf('GQCList') != -1) { //供求

    // }
    // 计算是不是超过3个
    // 选中未选中效果
    // 点击计算已选数量
    if (biaoqianList[index].List[index2].isSelect == true) {
      console.log('未选中--')
      biaoqianList[index].List[index2].isSelect = !biaoqianList[index].List[index2].isSelect
      selectNum--
      this.setData({
        biaoqianList,
        selectNum
      })
    } else {
      let filterList = biaoqianList[index].List.filter((item) => {
        return item.isSelect == true
      })
      if (filterList.length > 2) {
        wx.showToast({
          title: '最多可选三个类别',
          icon: 'none'
        })
        return
      } else if (biaoqianList[index].List[index2].isSelect) {
        if (biaoqianList[index].List[index2].isSelect == true) {
          selectNum++
        } else {
          selectNum--
        }
        biaoqianList[index].List[index2].isSelect = !biaoqianList[index].List[index2].isSelect
      } else {
        biaoqianList[index].List[index2].isSelect = true
        selectNum++
      }
      this.setData({
        biaoqianList,
        selectNum
      })
    }

  },
  close() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 添加标签
  addBiaoqian(e) {
    let that = this,
      List = that.data.biaoqianList,
      ZCCList = [],
      GQCList = [],
      GGCList = [],
      LoginUID = wx.getStorageSync('FjLoginLog').UID
    // let a = biaoqianList.filter((item, index) => {
    //   return item.List.filter((value, ind) => {
    //     return value.isSelect == true
    //   })
    // })
    if (that.data.selectNum == 0) {
      wx.showToast({
        title: '请先选择标签',
        icon:'none'
      })
      return
    }else {
      List.forEach((item) => {
        item.List.map(value => {
          if (item.Type == 'ZCCList') {
            if (value.isSelect == true) {
              // delete value.isSelect
              ZCCList.push(value)
            }
          } else if (item.Type == "GQCList") {
            if (value.isSelect == true) {
              // delete value.isSelect
              GQCList.push(value)
            }
          } else if (item.Type == "GGCList") {
            if (value.isSelect == true) {
              // delete value.isSelect
              GGCList.push(value)
            }
          }
        })
      })
    }

    app.getDaoPost('api/FeijiuApp/Feijiu_ADD_TagList', {
      LoginUID: wx.getStorageSync('FjLoginLog').UID,
      ZCCList: JSON.stringify(ZCCList).toString(),
      GQCList: JSON.stringify(GQCList).toString(),
      GGCList: JSON.stringify(GGCList).toString() 
    }, r => {
      console.log(r, '查询标签')
      if (r.code == 200) {
        wx.showToast({
          title: r.data,
          icon: 'none'
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }, 1200)
      } else {
        wx.showToast({
          title: r.Message || r.message,
          icon: 'none'
        })
      }
    })
  },
})